/*
 * MedicationView.fx
 *
 * Created on Mar 11, 2010, 9:46:37 PM
 */

package GraphsWindowPackage.MedicationPackage;

import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Math;
import GraphsWindowPackage.LabMedNode;
import GraphsWindowPackage.EntryView;

/**
 * @author quang
 */

public class MedicationView extends EntryView {
    var medication = bind (entry as Medication).createEntryInTimeRange(startShownTime, endShownTime);

    function createShapes(orders: Order[]) {
        var shapes: Node[];
        var startTimeCoord: Number;
        
        for (i in [0..sizeof orders - 1]) {
            startTimeCoord = (orders[i].startTime - startShownTime) * this.numPixPerTimeUnit;
            
            if (orders[i].isOnce)   //this order is one time prescreption
            {
                insert LabMedNode {
                    shape: Circle {
                        centerX: startTimeCoord
                        centerY: circleRadius
                        radius: circleRadius
                        fill: Color.BLACK
                        opacity: 0.5
                        scaleX: 0.4
                    }
                    info: "Time (once): {Math.round(Math.floor(orders[i].startTime))} days {Math.round ((orders[i].startTime - Math.floor(orders[i].startTime)) * 24)} hours from beginning of treatment period\n"
                            "Info: {orders[i].info}"
                } into shapes
            }
            else //this order is prescreption in a time period
            {
                insert LabMedNode {
                    shape: Rectangle {
                        x: startTimeCoord
                        y: 0
                        width: (orders[i].finishTime - orders[i].startTime) * this.numPixPerTimeUnit;
                        height: timeLineStrokeWidth
                        fill: Color.BLUE
                        opacity: 0.4
                        smooth: false
                    }
                    info: "Time period: {Math.round(Math.floor(orders[i].startTime))} days {Math.round ((orders[i].startTime - Math.floor(orders[i].startTime)) * 24)} hours -> "
                            "{Math.round(Math.floor(orders[i].finishTime))} days {Math.round ((orders[i].finishTime - Math.floor(orders[i].finishTime)) * 24)} hours (Time from beginning of treatment period)\n"
                            "Info: {orders[i].info}"
                } into shapes
            }
        }
        return shapes;
    }

    public override function create(): Node {
        var shapes = bind createShapes(this.medication.orders);

        def bgRect = Rectangle {
            x: 0, y: 0
            width: timeLineWidth, height: timeLineStrokeWidth
            fill: Color.LIGHTGRAY
            smooth: false
        }
        
        return HBox {
            content: bind if (shown) [nameField, Group { content: bind [bgRect, shapes] }]
                        else []
        }
    }
}
